package com.kazu;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.TemplateConfig;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;

/**
 * @Author kazu
 * @Date 2023/2/19 9:53
 * @Version 1.0
 */
@SuppressWarnings({"all"})
public class CodeGenerator {
	
	/*修改*/
	private String url = "jdbc:mysql://sh-cdb-ctmtn9ig.sql.tencentcdb.com:63801/kazu_blog";
	private String username = "root";
	private String password = "zzy121300";
	
	@Test
	public void run() {
		List<String> tables = new ArrayList<>();	/*修改*/
//		tables.add("kazu_comment_info");
		tables.add("kazu_skill_info");
//		tables.add("kazu_about_sentence");
		
		FastAutoGenerator.create(url,username,password)
				.globalConfig(builder -> {
					builder.author("kazu")
							.outputDir(System.getProperty("user.dir")+"\\src\\main\\java")
							.enableSwagger()
							.commentDate("yyyy/MM/dd HH:mm")
							.fileOverride();
					
				})
				.packageConfig(builder -> {
					builder.parent("com.kazu")
							.moduleName("skill")		/*修改*/
							.entity("entity")
							.service("service")
							.serviceImpl("service.impl")
							.controller("controller")
							.mapper("mapper")
							.xml("mapper")
							.pathInfo(Collections.singletonMap(OutputFile.xml,System.getProperty("user.dir")+
									"\\src\\main\\resources\\mapper"));
				})
				.strategyConfig(builder -> {
					builder.addInclude(tables)
							.addTablePrefix("kazu_")		/*修改*/
							.serviceBuilder()
							.formatServiceFileName("%sService")
							.formatServiceImplFileName("%sServiceImpl")
							.entityBuilder()
							.enableLombok()
							.logicDeleteColumnName("is_deleted")
							.enableTableFieldAnnotation()
							.controllerBuilder()
							// 映射路径使用连字符格式，而不是驼峰
							.enableHyphenStyle()
							.formatFileName("%sController")
							.enableRestStyle()
							.mapperBuilder()
							//生成通用的resultMap
							.enableBaseResultMap()
							.superClass(BaseMapper.class)
							.formatMapperFileName("%sMapper")
							.enableMapperAnnotation()
							.formatXmlFileName("%sMapper");
				})
				.templateConfig(new Consumer<TemplateConfig.Builder>() {
					@Override
					public void accept(TemplateConfig.Builder builder) {
						// 实体类使用我们自定义模板
						builder.entity("templates/myentity.java");
					}
				})
				.templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
				.execute();
	}
	
}
